package cn.xz.gobang.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import cn.xz.gobang.R
import cn.xz.gobang.base.BaseActivity
import cn.xz.gobang.base.BaseApplication
import cn.xz.gobang.bean.CommonLib
import cn.xz.gobang.bean.UserData
import cn.xz.gobang.databinding.ActivitySettingBinding
import cn.xz.gobang.dialog.CacheDialog
import cn.xz.gobang.dialog.EditNameDialog
import cn.xz.gobang.dialog.FkDialog
import cn.xz.gobang.net.OkHttpUtil
import cn.xz.gobang.util.*
import okhttp3.FormBody
import okhttp3.RequestBody

/**
 * 设置*/
class SettingActivity: BaseActivity<ActivitySettingBinding>(R.layout.activity_setting) {

    //用户信息
    var mBean: UserData? = null

    override fun initData() {
        //获取用户名
        getUserData()
        //音效开关
        getVoice()
        //是否显示对手数据
        getShowPk()
        //清除缓存
        getCache()
        //关于我们
        getAbout()
        //联系我们
        //退出APP
    }

    override fun initEvent() {
        //音效
        mDataBind.llcVoice.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.OPEN_VOICE,!SessionUtils.getBoolean(SessionUtils.OPEN_VOICE,true))
            getVoice()
        }
        //显示数据
        mDataBind.llcShowPk.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.SHOW_PK_DATA,!SessionUtils.getBoolean(SessionUtils.SHOW_PK_DATA,true))
            getShowPk()
        }
        //缓存
        mDataBind.llcCache.setOnClickListener {
            CacheDialog.newInstance(object :CacheDialog.OnClickListener{
                override fun onYes() {
                    DataCleanManager.clearAllCache(this@SettingActivity)
                    getCache()
                }
            }).show(this.supportFragmentManager,"CacheDialog")
        }
        //联系我们
        mDataBind.llcCallMine.setOnClickListener {
            val clipboard: ClipboardManager = this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            clipboard.setPrimaryClip(ClipData.newPlainText(null,"zch15753804892"))
            ToastUtil.show("作者微信复制成功,快去添加吧~")
        }
        //退出
        mDataBind.llcBey.setOnClickListener {
            val mList = BaseApplication.getActivityList()
            mList?.forEach {
                it.finish()
            }
        }
        //返回
        mDataBind.ivBack.setOnClickListener {
            finish()
        }
    }

    private fun getUserData(){
        //获取用户ip转化为用户名
        mDataBind.tvUserName.text = CommonLib.UserIP
    }

    //获取缓存
    private fun getCache(){
        mDataBind.tvCache.text = "当前缓存: " + DataCleanManager.getTotalCacheSize(this)
    }

    //关于我们
    private fun getAbout(){
        mDataBind.tvAbout.text = "当前版本号: V${AppHelper.getAppVersionName()}"
    }

    //音效是否开
    private fun getVoice(){
        var mD = AppHelper.getDrawable(R.mipmap.icon_k)
        if (!SessionUtils.getBoolean(SessionUtils.OPEN_VOICE,true)){
            mD = AppHelper.getDrawable(R.mipmap.icon_g)
        }
        mDataBind.ivVoice.setImageDrawable(mD)
    }

    //是否显示对手数据
    private fun getShowPk(){
        var mD = AppHelper.getDrawable(R.mipmap.icon_k)
        if (!SessionUtils.getBoolean(SessionUtils.SHOW_PK_DATA,true)){
            mD = AppHelper.getDrawable(R.mipmap.icon_g)
        }
        mDataBind.ivShowPk.setImageDrawable(mD)
    }
}